package GUI;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Vector;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JComboBox;

import Common.Course;
import java.awt.event.ActionEvent;
/**
 * 
 * @author AlonGluzman
 *
 */

public class AAViewNewAssignmentScreen extends JPanel implements Serializable{
  
	public StudentMainMenu stmn = null;
	public Login myLoginFrame = null;
	public UserStudent user = null;
	public String cID;
	
	private String IsExceptional = "Exceptional request approved";
    private int index=0;
    String getAnsewrS = null;	
    byte [] byteArray = null;
    public String taskName=null;
    public String deadLinee=null;
    public byte [] willBeFIle=null;
	private JFileChooser fc;	                  /// for saving a file menu
	
	
	private JButton btnViewAssignment=null;
    private JComboBox selCourseCb = null;
	

   //making a button for getting an assignment file
	public JButton getFIleBtn()
	{
		if(btnViewAssignment==null)
		{
			btnViewAssignment = new JButton("View Assignment");
			 fc= new JFileChooser();															
			btnViewAssignment.addActionListener(new java.awt.event.ActionListener()
			{
				//using a JFileChooser to choose where to save the new assignment on the client side
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					int returnVal = fc.showSaveDialog(AAViewNewAssignmentScreen.this);
					if (returnVal == JFileChooser.APPROVE_OPTION)
					{
						//Geting the File
							try{       
					         File pathFile = fc.getSelectedFile();
					        //making the a file
					    	OutputStream getfile = null;
							try 
							{
								getfile = new FileOutputStream(pathFile.getAbsolutePath());
							} catch (FileNotFoundException e1)
							{
					            System.out.println("ERROR Saving the file 2");   
								e1.printStackTrace();
							}	   
					    	try 
					    	{
								getfile.write(willBeFIle);
							} catch (IOException e2)
							{
					            System.out.println("ERROR Saving the file 3");   
								e2.printStackTrace();
							}
					    	System.out.println(willBeFIle.length);
								 getfile.close();
							}
    
							catch (Exception e3) 
					{
						 System.out.println("ERROR Saving the file");
					}
					}
				}
			});		
				
				
			}		
			btnViewAssignment.setBounds(493, 230, 115, 23);
			return btnViewAssignment;
	}
	
	//making a combo box for course details
	public JComboBox getSelCourseCb()
	{

		Vector<String> courses = new Vector<String>();
		courses.add(cID+" "+taskName+" "+deadLinee);

		if(selCourseCb==null)
		{

		 selCourseCb = new JComboBox();

		 selCourseCb = new JComboBox(courses);

		 selCourseCb.addActionListener(new ActionListener() {
		 
			 public void actionPerformed(ActionEvent e) {
				
		 		//What happends when a user chooses a file in the CB
		 	}
		 });
		selCourseCb.setBounds(201, 120, 219, 22);
			
		}
		return selCourseCb;
	}
	
	
	private void initialize()
	{
		setLayout(null);
		JLabel lblSelectCourse = new JLabel("Select course:");
		lblSelectCourse.setBounds(102, 124, 68, 14);
		add(lblSelectCourse);
		add(getFIleBtn());
		add( getSelCourseCb());

	}
	//Constructor get the details from the DB
	public AAViewNewAssignmentScreen(StudentMainMenu stmn) {
    	super();
      	 user=new UserStudent();
         //getting ClassId 
        String classID = stmn.user.StudentData.ClassId;

        System.out.println("ViewNewAssignmentScreen q\n");
        while (index<stmn.user.StudentData.ListOfCourses.lOfCourses.size()){				//	Getting all the couurse
        	cID =stmn.user.StudentData.ListOfCourses.lOfCourses.get(index).getCid()+" ";	// index will tell how many courses there
       	index++;																		// are for the Student
             }
        System.out.println("print Course : "+cID);
        System.out.println("ViewNewAssignmentScreen\nClass ID IS : "+classID+"\nCid is : "+cID);
 

        myLoginFrame.client.handleMessageFromClientUI("viewNewAssignmentDetails "+classID+" "+cID);
        try {

        	Thread.sleep(1000);

		} catch (InterruptedException e) {
			System.out.println("ViewNewAssignmentScreen THREAD ");
			e.printStackTrace();
		}
        taskName = stmn.getTaskNum();
        deadLinee = stmn.getDeadLine();
         willBeFIle= stmn.getMyByteArr();
         initialize();   

}}
